﻿
@{
    Layout = null;
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <!--Script references. -->

</head>
<body>
    <p>
        @ViewBag.ClientName<br>
        @ViewBag.ClientID
    </p>
    <p>
        @*<span>房间号：</span>
            <input type="text" id="groupId" />*@
        <span>用户名：</span>
        <input type="text" id="userName" />
        <button id="joinRoom">加入聊天室</button>
    </p>
    <p>
        <span>消息：</span>
        <input type="text" id="message" />
        <button id="send">发送</button>
    </p>
    <p>
        <span>消息：</span>
        <input type="text" id="msg" />
        <input type="text" id="userId" />
        <button id="TestSend">发送指定用户</button>
    </p>
    <div>
        <ul id="contentMsg"></ul>
    </div>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.signalR-2.2.1.min.js"></script>
    <script src="http://localhost:4098/signalrserver/hubs"></script>
    <script type="text/javascript">
        $(function () {
            var userId=@ViewBag.ClientID;
            var userName='@ViewBag.ClientName';
            var chat = $.connection.chatHub;
            chat.hubName = 'chatHub';
            chat.connection.start().done(function () {
                //调用服务端函数Connect（首字母小写）以及传递客户端参数进行上线操作
                chat.server.connect(userId, userName);
            });

            chat.client.onConnected=function(id,userName,ConnectedUsers){
                $("#contentMsg").append("<li>登录成功：" + userName + ": " + id + "</li>");
            }

            chat.client.receivePrivateMessage=function(fromUserId,UserName,message){
                $("#contentMsg").append("<li>" + UserName + ": " + message + "</li>");
            }

            chat.client.addSomeMessage = function (groupId, detail, userName) {
                console.info("广播消息：" + detail);
                $("#contentMsg").append("<li>" + userName + ": " + detail + "</li>");
            };

            chat.client.addUserIn = function (groupId, userName) {
                $("#contentMsg").append("<li>" + userName + "进入该聊天室！</li>");
            };

            chat.client.sendAppoint = function (fromUserId, userId, detail) {
                $("#contentMsg").append("注意：这是发给：" + fromUserId);
            };

            chat.client.allReceiveUser = function (detail) {
                $("#contentMsg").append("所有人注意：这是通知" + detail);
            };

            chat.client.onUserDisconnected = function (ConnectionId, UserName) {
                $("#contentMsg").append("用户：" + UserName + "下线了");
            }
            chat.client.onNewUserConnected=function(id,userId,userName,date){
                $("#contentMsg").append("<li>" + userName + "上线了！</li>");
            }
            $.connection.hub.logging = true;//启动signalr状态功能

            //加入聊天室
            $("#joinRoom").click(function () {
                var groupId = 1;//$("#groupId").val();
                var userName = $("#userName").val();
                chat.server.addToRoom(groupId, userName);
            });
            //发送消息
            $("#send").click(function () {
                var detail = $("#message").val();
                //var groupId = 1;// $("#groupId").val();
                //var userName = $("#userName").val();
                //var date = getNowFormatDate();
                //detail += "|" + date;
                //chat.server.send(groupId, detail, userName);
                //$("#message").val("");

                chat.server.sendAll(detail);
              //  chat.server.sendAppointUser("1000", "你好啊。1000");
            });

            $("#TestSend").click(function () {
                var msg = $("#msg").val();
                var userId = $("#userId").val();
                chat.server.sendAppointUser(userId, msg);


                //var time = 110000060;
                //var mTime = setInterval(function () {
                //    if (time > 0) {
                //        var detail = $("#message").val();
                //        var groupId = 1;
                //        var userName = $("#userName").val();
                //        chat.server.send(groupId, detail, userName);
                //    } else {
                //        clearInterval(mTime);
                //    }
                //    time--;
                //}, 1);
            });
        });
        function getNowFormatDate() {
            var date = new Date();
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                    + " " + date.getHours() + seperator2 + date.getMinutes()
                    + seperator2 + date.getSeconds();
            return currentdate;
        }
    </script>
</body>
</html>